﻿

/*************************************************
*   String Utilites    ***************************
**************************************************/
function stripHTML(originalStr) {

    stripHTML(originalStr, "");
}

function stripHTML(originalStr, replacerStr) {
    var regex = /<\S[^><]*>/g;
    return originalStr.replace(regex, replacerStr);
}

function isValidEmail(sText) {
    var regexEmail = /^(?:\w+\.?)*\w+@(?:\w+\.)+\w+$/;
    return regexEmail.test(sText);
}

function isValidUrl(originalStr) {
    var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
    return regexp.test(originalStr);
}


/********** String Trim Utilites *********************/

String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, "");
}
String.prototype.ltrim = function() {
    return this.replace(/^\s+/, "");
}
String.prototype.rtrim = function() {
    return this.replace(/\s+$/, "");
}

/********** String.Format Utilites *********************/
String.format = function(text) {

    //check if there are two arguments in the arguments list
    if (arguments.length <= 1) {
        //if there are not 2 or more arguments there's nothing to replace
        //just return the original text
        return text;
    }

    //decrement to move to the second argument in the array
    var tokenCount = arguments.length - 2;
    for (var token = 0; token <= tokenCount; token++) {
        //iterate through the tokens and replace their placeholders from the original text in order
        text = text.replace(new RegExp("\\{" + token + "\\}", "gi"),
                                                arguments[token + 1]);
    }
    return text;
};

/*********************************************************************************************
*********** HTTP Request Utilites ************************************************************
**********************************************************************************************/

function GetHttpRequestObject() {

    var xmlhttp = false;

    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
            xmlhttp = false;
        }
    }

    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        try {
            xmlhttp = new XMLHttpRequest();
        } catch (e) {
            xmlhttp = false;
        }
    }
    if (!xmlhttp && window.createRequest) {
        try {
            xmlhttp = window.createRequest();
        } catch (e) {
            xmlhttp = false;
        }
    }
    return xmlhttp;
}



/*********************************************************************************************
*********** General JS Utils           ************************************************************
**********************************************************************************************/


function GetElementValue(eid) {
    return document.getElementById(eid).value;
}

function SetElementValue(eid, val) {
    document.getElementById(eid).value = val;
}


/*********************************************************************************************
*********** Cookie Utilities           ************************************************************
**********************************************************************************************/


function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

/*********************************************************************************************
*********** Reading query string parameter           ************************************************************
**********************************************************************************************/

function getQueryStringValue(parameterName) {
  var objQRs = new Object();
  window.location.search.replace(new RegExp("([^?=&]+)(=([^&]*))?", "g"),
          function($0, $1, $2, $3) { objQRs[$1] = $3; });
  return objQRs[parameterName]
}  